//
//  HotelReservationViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/7/1.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "HotelReservationViewController.h"
#import "HotelReservationTableViewCell.h"
#import "YMDDateAlertView.h"

static NSString *cellIdentifier = @"HotelReservationTableViewCell";

@interface HotelReservationViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UILabel *beginLabel;//入住日期
@property (strong, nonatomic) IBOutlet UILabel *endLabel;//离开日期
@property (strong, nonatomic) IBOutlet UILabel *totalLabel;//共%@晚
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation HotelReservationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initViewLayout];
}

- (void)initViewLayout {
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    
    [self.tableView registerNib:[UINib nibWithNibName:cellIdentifier bundle:nil] forCellReuseIdentifier:cellIdentifier];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}


- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}

#pragma mark === 开始时间
- (IBAction)choseBeginTime:(id)sender {
    YMDDateAlertView * alertView = [[YMDDateAlertView alloc]initWithTitle:@"请选择入住日期" sureButtonTitle:@"确定" limitDate:YES dateType:0 complect:^(NSString *str) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
        
        NSDate * begindate = [formatter dateFromString:str];
        NSDate * enddate = [formatter dateFromString:[_endLabel.text substringFromIndex:6]];
        if (_endLabel.text.length == 16) {
            if ([begindate compare:enddate]==NSOrderedDescending) {
                [CWHUDTips showTips:@"入住时间不得晚于离开时间"];
            }else{
                _totalLabel.hidden = NO;
                _beginLabel.text = [NSString stringWithFormat:@"入住日期  %@",str];
                NSCalendar *calendar = [NSCalendar currentCalendar];
                NSCalendarUnit unit = NSCalendarUnitDay;
                NSDateComponents *dateCom = [calendar components:unit fromDate:begindate toDate:enddate options:0];
                _totalLabel.text = [NSString stringWithFormat:@"共%ld晚",dateCom.day];
            }
        }else{
            _beginLabel.text = [NSString stringWithFormat:@"入住日期  %@",str];
        }
    }];
    [alertView show];
}

#pragma mark === 选择结束时间
- (IBAction)choseEndTime:(id)sender {
    YMDDateAlertView * alertView = [[YMDDateAlertView alloc]initWithTitle:@"请选择离开日期" sureButtonTitle:@"确定" limitDate:YES dateType:0 complect:^(NSString *str) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
        
        NSDate * begindate = [formatter dateFromString:[_beginLabel.text substringFromIndex:6]];
        NSDate * enddate = [formatter dateFromString:str];
        if (_beginLabel.text.length==16) {
            if ([enddate compare:begindate]==NSOrderedAscending) {
                [CWHUDTips showTips:@"离开时间不得早于入住时间"];
            }else{
                _totalLabel.hidden = NO;
                _endLabel.text = [NSString stringWithFormat:@"离开日期  %@",str];
                NSCalendar *calendar = [NSCalendar currentCalendar];
                NSCalendarUnit unit = NSCalendarUnitDay;
                NSDateComponents *dateCom = [calendar components:unit fromDate:begindate toDate:enddate options:0];
                _totalLabel.text = [NSString stringWithFormat:@"共%ld晚",dateCom.day];
            }
        }else{
            _endLabel.text = [NSString stringWithFormat:@"离开日期  %@",str];
        }
    }];
    [alertView show];
    
    
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//定义 UITableView cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

//定义 UITableView cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 94*VerticalRatio();
}

//定义 UITableView cell 展示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HotelReservationTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell= [[HotelReservationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    
    return cell;
}

//定义 UITableView cell 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
